/*
 * Created on 12-Jul-06 Filename: ThanksCommand.java @author Daniel Yule
 */
package com.danicsoft.daide.command;

import com.danicsoft.daide.token.StandardToken;
import com.danicsoft.daide.token.TokenGroup;
import com.danicsoft.daide.token.TokenMap;
import com.danicsoft.daide.token.TokenMap.Commands;

/**
 * Implements the THX command. From Eric Wald's <a
 * href="http://pydip.brainshell.org/docs/syntax.html">protocol specification</a>,
 * v0.14:
 * <h4><a name="THX">server_message = THX</a> (order) (note)</h4>
 * <p>
 * <b>order</b> is the order as submitted. <b>note</b> is one of the
 * following:
 * </p>
 * <ul>
 * <li><b>note = MBV</b> &mdash; Order is okay</li>
 * <li><b>note = FAR</b> &mdash; Not adjacent</li>
 * <li><b>note = NSP</b> &mdash; No such province</li>
 * <li><b>note = NSU</b> &mdash; No such unit</li>
 * <li><b>note = NAS</b> &mdash; Not at sea (for a convoying fleet)</li>
 * <li><b>note = NSF</b> &mdash; No such fleet (in <b>VIA</b> section of
 * <b>CTO</b> or the unit performing a <b>CVY</b>)</li>
 * <li><b>note = NSA</b> &mdash; No such army (for unit being ordered to
 * <b>CTO</b> or for unit being <b>CVY</b>ed)</li>
 * <li><b>note = NYU</b> &mdash; Not your unit</li>
 * <li><b>note = NRN</b> &mdash; No retreat needed for this unit</li>
 * <li><b>note = NVR</b> &mdash; Not a valid retreat space</li>
 * <li><b>note = YSC</b> &mdash; Not your supply centre</li>
 * <li><b>note = ESC</b> &mdash; Not an empty supply centre</li>
 * <li><b>note = HSC</b> &mdash; Not a home supply centre</li>
 * <li><b>note = NSC</b> &mdash; Not a supply centre</li>
 * <li><b>note = CST</b> &mdash; No coast specified for fleet build in a
 * multi-coastal province, or an attempt to build a fleet inland or an army at
 * sea.</li>
 * <li><b>note = NMB</b> &mdash; No more builds allowed</li>
 * <li><b>note = NMR</b> &mdash; No more removals allowed</li>
 * <li><b>note = NRS</b> &mdash; Not the right season</li>
 * </ul>
 * <p>
 * Alternatively, it will reply with <b>REJ (SUB (order) (order) ...)</b> if
 * the game has not started.
 * </p>
 * <p>
 * Any order which receives a note other than <b>MBV</b> has been rejected by
 * the server, and a corrected order should be submitted in its place.
 * </p>
 * 
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 */
public class ThanksCommand extends Command {

	/**
	 * The order we are being thanked for.
	 */
	private OrderSpec order;

	/**
	 * The note associated with this order.
	 */
	private StandardToken note;

	/**
	 * Constructs a new <code>ThanksCommand</code> from the tokens given.
	 * 
	 * @param tokens
	 *        A <code>TokenGroup</code> with two subgroups: the first is the
	 *        order concerned, the second is the note we have recieved for this
	 *        order.
	 * @throws ParseException
	 *         If there was a problem parsing the tokens given.
	 */
	public ThanksCommand(TokenGroup tokens) throws ParseException {
		super(tokens);
	}

	/**
	 * Constructs a new <code>ThanksCommand</code> for being sent to the
	 * client.
	 * 
	 * @param order
	 *        The order we are thanking the client for.
	 * @param note
	 *        The note we are sending with this order.
	 */
	public ThanksCommand(OrderSpec order, int note) {
		this.order = order;
		this.note = TokenMap.getNoteToken(note);
	}

	@Override
	public byte getType() {
		return Commands.THX;
	}

	@Override
	public String toString() {
		// TODO make this return a string based on the order note
		return "Thanks for your order!";
	}

	public void doIO(TokenIO tokenIO, TokenGroup tokens) throws ParseException {
		order.doIO(tokenIO, tokens);
		note = (StandardToken) tokenIO.doToken(tokenIO.subGroup(tokens), note, TokenMap.order_notes);

	}

	@Override
	public void initialize() {
		order = new OrderSpec();
		super.initialize();
	}

	/**
	 * Gets the note for this order
	 * 
	 * @return The note
	 */
	public byte getNote() {
		return note.getSubType();
	}

	/**
	 * Gets the order that was submitted.
	 * 
	 * @return The order
	 */
	public OrderSpec getOrder() {
		return order;
	}

}
